/* Load combos from colums*/
function loadTableColumCombo(colum, select) {

   removeAllChildNodes(select);
   var options = getVisibleOptions(colum);
   for(var j=0; j<options.size(); j++) {
      addOption(select, options[j])
   }
}

function getVisibleOptions(colum) {
   var otps = new Array();
   var rows = document.getElementById(filterManager.tableId).tBodies[0].rows;
   for(var i=0; i<rows.length; i++) {
      if(rows[i].style.display!='none') {
         otps.push(rows[i].cells[colum].innerHTML);
      }
   }
   opts = otps.removeDuplicate();
   opts.sort();
   return opts;
}


function ColumnFilter(column, value) {
   this.column = colum
   this.value = value
}


function filterTable(colum, select) {
   filterManager.updateFilter(colum, select)

   var rows = document.getElementById(filterManager.tableId).tBodies[0].rows;

   var hide;

   for(var i=0; i<rows.length; i++) {
      if(filterManager.validate(rows[i])) {
         hide=""
      } else {
         hide="none"
      }
      rows[i].style.display=hide;
      try {
        //hide relation if exists
        var relation = rows[i].getAttribute('id');
        var elem_tr = document.getElementById('r'+relation);
        elem_tr.style.display=hide;
      }catch(e) {}
   }

   filterManager.reloadSelects();
}

function FilterManager(tableId) {
   this.filters = new Array(); // Map
   this.tableId = tableId;
}

FilterManager.prototype.updateFilter = function(colum, select) {
   if(select.value=="todo") {
      this.filters[colum]= new Filter(colum, 'todo', select)
   } else {
      this.filters[colum] = new Filter(colum,select.options[select.selectedIndex].text, select);
   }
}

FilterManager.prototype.validate = function(row) {
   var valid=true;
   for(var i=0; i < this.filters.size(); i++) {
     if(this.filters[i] != undefined) {
        if(this.filters[i].value != "todo") {
           if(!this.filters[i].isValid(row)) return false
           valid = true;
        }
     }
   }
   return valid;
}

FilterManager.prototype.reloadSelects = function() {
   for(var i=0; i < this.filters.size(); i++) {
   if(this.filters[i] != undefined) {
        this.filters[i].reloadSelect();
        }
   }
}

FilterManager.prototype.toString = function() {
   return "FilterManager with "+this.filters.size()+" filters";
}

function Filter(colum, value, select) {
   this.colum = colum;
   this.value = value;
   this.select = select
}

Filter.prototype.isValid = function(row) {
   return (row.cells[this.colum].innerHTML==this.value);
}

Filter.prototype.reloadSelect = function() {
   if(this.value=='todo') {
      this.select.loadOptions();
      this.select.value=this.value
   }
}

Filter.prototype.toString = function() {
   return "Filter"
}

function initFilterSelect(idSelect, title, column) {
   var select = document.getElementById(idSelect)
   select.loadOptions = function () {
                                  loadTableColumCombo(column, this);
                                  insertOption(this, title,'todo')
                                }
  select.loadOptions();

  filterManager.updateFilter(column,select);
}


